#[cfg(test)]
mod tests {
    use super::*;
    use actix_web::{test, web, App};
    use sea_orm::{Database, DatabaseConnection};
    use std::sync::Arc;
    use uuid::Uuid;

    async fn setup_test_db() -> DatabaseConnection {
        let db = Database::connect("sqlite::memory:")
            .await
            .expect("Failed to connect to test database");
        
        // Run migrations
        crate::migrations::run_migrations(&db)
            .await
            .expect("Failed to run test migrations");
        
        db
    }

    async fn create_test_app() -> impl actix_web::dev::Service<
        actix_web::dev::ServiceRequest,
        Response = actix_web::dev::ServiceResponse,
        Error = actix_web::Error,
    > {
        let db = setup_test_db().await;
        let config = crate::config::Config::from_env().unwrap();
        let cache = crate::cache::AppCache::new(100, 60);
        
        let auth_service = crate::auth::AuthService::new(
            db.clone(),
            cache.clone(),
            config.oidc_issuer,
            config.oidc_client_id,
            &config.oidc_jwks_url,
        )
        .await
        .unwrap();

        let jwt_validator = auth_service.jwt_validator.clone();

        test::init_service(
            App::new()
                .app_data(web::Data::new(db))
                .app_data(web::Data::new(auth_service))
                .app_data(web::Data::new(cache))
                .app_data(crate::aliri_actix::jwt::validator(jwt_validator))
                .configure(crate::routes::configure_routes),
        )
        .await
    }

    #[actix_web::test]
    async fn test_upload_resources() {
        let app = create_test_app().await;

        // Create a multipart form with test file
        let payload = actix_multipart::Multipart::new(
            actix_web::http::header::ContentType::form_url_encoded(),
            "test content".as_bytes(),
        );

        let req = test::TestRequest::post()
            .uri("/api/admin/resources/upload")
            .to_request();

        let resp = test::call_service(&app, req).await;
        // This test needs actual multipart data to work properly
        assert_eq!(resp.status(), actix_web::http::StatusCode::BAD_REQUEST);
    }

    #[actix_web::test]
    async fn test_get_nonexistent_resource() {
        let app = create_test_app().await;

        let req = test::TestRequest::get()
            .uri(&format!("/api/admin/resources/{}", Uuid::new_v4()))
            .to_request();

        let resp = test::call_service(&app, req).await;
        assert_eq!(resp.status(), actix_web::http::StatusCode::NOT_FOUND);
    }

    #[actix_web::test]
    async fn test_delete_nonexistent_resource() {
        let app = create_test_app().await;

        let req = test::TestRequest::delete()
            .uri(&format!("/api/admin/resources/{}", Uuid::new_v4()))
            .to_request();

        let resp = test::call_service(&app, req).await;
        assert_eq!(resp.status(), actix_web::http::StatusCode::NOT_FOUND);
    }
}